package com.lin.service.impl;

import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;

import com.lin.common.exception.BaseException;
import com.lin.service.SendMsgService;
import com.lin.utils.CodeUtil;
import com.lin.utils.SmsTool;
import org.springframework.stereotype.Service;



@Service
public class SendMsgServiceImpl implements SendMsgService {

    public String sendMsg(String phone) {
        try {

            //生成验证码并指定长度
            String code = CodeUtil.generateVerifyCode(6);
            //多个变量
            //String TemplateParam = "{\"name\":\""+name+"\",\"time\":\""+time+"\"}";
            String TemplateParam = "{\"code\":\""+code+"\"}";
            // 短信模板id
            String TemplateCode = "SMS_464235329";
            //发送短信
            SendSmsResponse response = SmsTool.sendSms(phone,TemplateParam,TemplateCode);
            if( response.getCode().equals("OK")) {
                return code;
            }else {
                throw new BaseException("发送失败");
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }
}
